#include "Headers/UI/advacedoptionaddeditdialog.h"
#include "ui_advacedoptionaddeditdialog.h"

#include "../../Headers/DataAccessors/advancedoptiondataaccessor.h"

#include <QTextCodec>

AdvacedOptionAddEditDialog::AdvacedOptionAddEditDialog(
        MyUtils::FormStatus formStatus,
        AdvancedOption * option,
        QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AdvacedOptionAddEditDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::Dialog |
                         Qt::WindowMinimizeButtonHint |
                         Qt::WindowMaximizeButtonHint |
                         Qt::WindowStaysOnTopHint);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    this->formStatus = formStatus;
    this->option = option;

    if (formStatus == MyUtils::Editing)
    {
        ui->createButton->setText(trUtf8("Изменить"));
        if (option != NULL)
        {
            ui->NameLineEdit->setText(option->getName());
            //ui->CostLineEdit->setText(QString::number(option->getCost()));
            ui->costDoubleSpinBox->setValue(option->getCost());
        }
    }
}

AdvacedOptionAddEditDialog::~AdvacedOptionAddEditDialog()
{
    delete ui;
}

void AdvacedOptionAddEditDialog::on_createButton_clicked()
{
    AdvancedOptionDataAccessor dataOption;
    AdvancedOption option;
    option.setName(ui->NameLineEdit->text());
    //option.setCost(ui->CostLineEdit->text().toDouble());
    option.setCost(ui->costDoubleSpinBox->value());
    if (formStatus == MyUtils::Adding)
    {
        dataOption.setAdvancedOption(option);
    }
    else if (formStatus == MyUtils::Editing)
    {
        option.setId(this->option->getId());
        dataOption.editAdvancedOption(option);
    }
    this->close();
}

void AdvacedOptionAddEditDialog::on_cancelButton_clicked()
{
    this->close();
}
